#coding=utf8
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.contrib.auth.models import User

from docshare.share.models import *
from docshare.share.tools import *
from docshare.share.forms import PubForm

import settings

def new(request,id=0):
    if id!=0:
        obj = pub.objects.get(pk=id)
    else:
        obj = None
    if request.method == 'POST':
        form = PubForm(request.POST,instance=obj)
        if form.is_valid():
            new_pub=form.save(commit=False)
            new_pub.user = request.user
            new_pub.save()
            return HttpResponseRedirect('/%s/affiche/list/'%settings.SITE_ROOT)
    else:
        if id==0:
            form=PubForm()
        else:
            obj = pub.objects.get(pk=id)
            form=PubForm(instance=obj)
    return render_to_response('affiche/new.html',{'form':form},context_instance=RequestContext(request))

def list(request):
    objects = pub.objects.all()
    if request.method == 'POST':
        chks = request.POST.getlist('chkID')
        for chk in chks:
            obj = pub.objects.get(pk=chk)
            obj.delete()
    return render_to_response('affiche/list.html',{'objects':objects},context_instance=RequestContext(request))

def affiche_sub(request):
    return render_to_response('affiche/affiche_sub.html',context_instance=RequestContext(request))

def show(request,id):
    obj = pub.objects.get(pk=id)
    return render_to_response('affiche/show.html',{'obj':obj},context_instance=RequestContext(request))

def dels(request,id):
    obj = pub.objects.get(pk=id)
    obj.delete()
    return HttpResponseRedirect('/%s/affiche/list/'%settings.SITE_ROOT)

